
//
// 110815 - AH - Created.
// 110321 - AH - Removed @Override for interface method (1.5 compatible).
//

package org.soekrat.gui.swing;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


/**
 * <p>
 *   Dialog that shows help information.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@SuppressWarnings("serial")
public final class HelpDialog extends JDialog 
{
  private JEditorPane m_edp=new JEditorPane();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public HelpDialog(){ buildGui(); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param p Dialog's parent.
   */
  public HelpDialog(Frame p){ super(p); buildGui(); }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param p Dialog's parent.
   */
  public HelpDialog(Dialog p){ super(p); buildGui(); }
  
  // Invoked from constructor.
  private void buildGui()
  {
    setTitle("Help");
    setModal(false);
    
    Container c=getContentPane();
    c.setLayout(new BorderLayout());
    
    m_edp.setEditable(false);
    c.add(new JScrollPane(m_edp));
    
    JPanel buttons=new JPanel(new FlowLayout(FlowLayout.CENTER));
    buttons.add
    (
      new JButton
      (
        new AbstractAction("Close") 
        {
          public void actionPerformed(ActionEvent e){ dispose(); }
        }
      )
    );
    c.add(buttons, BorderLayout.SOUTH);
    
    m_edp.setContentType("text/html");
    load();
    
    pack();
    setSize(500, 500);
  }
  
  private void load()
  {
    URL url=ClassLoader.getSystemResource("org/soekrat/gui/swing/help.html");
    if (url!=null)
    {
      InputStream is=null;
      try
      {
        is=url.openStream();
        m_edp.read(is, null);
      }
      catch (IOException iox)
      {
        iox.printStackTrace();
      }
      finally
      {
        if (is!=null) try { is.close(); } catch (IOException _){}
      }
    }
  }
  
  /**
   * <p>
   *   Test program to check the help dialog.
   * </p>
   * @param args Command line arguments, not used.
   */
  public static void main(String[] args) 
  {
    TestFrame.show(new HelpDialog());  
  }
  
}
